
#!/usr/bin/python3
# _*_ coding: utf-8 _*_
# @Time    : 2023/12/2 18:22
# @Author  : gsunwu@163.com
# @File    : gitlab_to_ali.py
# @Description: 
"""
download file from pan of ali

usage:
pip install aligo

down file to ./ in alipan where located in auto_backup/backup/uts_datas/benchmark.zip 

/Users/sunwu/miniforge3/envs/sw_tf_2_10/bin/python  ali_down.py --download_file auto_backup/backup/uts_datas/benchmark.zip  

"""

import traceback
from aligo import Aligo

def download_from_ali(download_file, local_folder):
    """

    Parameters
    ----------
    local_dir :  本地路径
    target_dir :  阿里网盘路径

    Returns
    -------

    """
    try:
        ali = Aligo()  # 第一次使用，会弹出二维码，供扫描登录

        user = ali.get_user()  # 获取用户信息
        print("用户信息", user.user_name, user.nick_name, user.phone)  # 打印用户信息
        print(f"下载文件夹 [{download_file}] ")
        target_dir_obj = ali.get_file_by_path(download_file)
        # print(target_dir_obj)
        # BaseFile(type='file', file_id='65d75f451fe148fca893495fbbcfbea43d4c60f2', name='benchmark.zip', parent_file_id='65d75f452ad9f2edfdf548f18fbf6a619700391d')
        if target_dir_obj is None:
            print(f"找不到阿里云盘的文件夹: {download_file}")
        else:
            # ali.download_folder(target_dir_obj.file_id)
            ali.download_file(file_id=target_dir_obj.file_id,local_folder=local_folder)
            print("download successful")
    except Exception as e:
        traceback.print_exc()

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--download_file",type=str,required=True,help="The directory you want to backup",default="")
    parser.add_argument("--local_folder",type=str,default="./downloads",help="The directory you want to backup")
    args = parser.parse_args()
    download_from_ali(args.download_file,args.local_folder)
